//
//  ViewController.m
//  ImageBrowse
//
//  Created by bike on 17/1/19.
//  Copyright © 2017年 bike. All rights reserved.
//

#import "ViewController.h"
#import "ImageBrowseTableViewCell.h"
#import "LBXImageBrowser.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, ImageBrowseTableViewCellImageTapDelegate, LBXImageBrowserDelegate>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *dataSource;
@property (strong, nonatomic) LBXImageBrowser *imageBrowser;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    _imageBrowser = [LBXImageBrowser new];
    _imageBrowser.delegate = self;
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
    [ImageBrowseTableViewCell registerInTableView:_tableView];
    _dataSource = @[@"1", @"2", @"3", @"4",];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImageBrowseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[ImageBrowseTableViewCell identifier] forIndexPath:indexPath];
    cell.delegate = self;
    [cell reloadWithImageName:_dataSource[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ImageBrowseTableViewCell cellHeight];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ImageBrowseTableViewCellImageTapDelegate

- (void)tapAtImageWithCell:(UITableViewCell *)cell
{
    NSIndexPath *indexPath = [_tableView indexPathForCell:cell];
    [_imageBrowser showImageAtIndex:indexPath.row];
}

#pragma mark - LBXImageBrowserDelegate
- (NSInteger)numberOfImagesForImageBrowser:(LBXImageBrowser *)browser
{
    return _dataSource.count;
}

- (UIImageView *)imageBrowser:(LBXImageBrowser *)browser imageViewAtIndex:(NSInteger)index
{
    ImageBrowseTableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    return cell.cellImageView;
}

- (UIImage *)imageBrowser:(LBXImageBrowser *)browser imageAtIndex:(NSInteger)index
{
    return [UIImage imageNamed:_dataSource[index]];
}

@end
